package org.itworks.biodev.gui.panels;

import java.awt.event.*;
import java.sql.*;
import java.util.Vector;

import javax.swing.JOptionPane;

import org.itworks.biodev.gui.*;
import org.itworks.biodev.model.*;

public class CustomersPanelControler  {
	
	private CustomersPanelModel model = null;
	
	
	public CustomersPanelControler(CustomersPanelModel model) {
		this.model = model;
	}

	public void CustomersPanelOnLoad() {
		try {
			this.model.setCustomers(CustomerTable.getInstance().findAll());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public CustomersPanelModel getModel() {
		return this.model;
	}

	/**
	 * Fonction permettant d'implemmenter l'action après avoir appuyuer sur "modfier"
	 * @param actionEvent
	 * @param selectedIndex index du client à modifier
	 */
	public void buttonColumnCustomersActionPerformed(ActionEvent actionEvent,
			int selectedIndex) {
		// TODO Auto-generated method stub
		try {
			Customer customerToEdit = this.model.getCustomers().elementAt(selectedIndex);
			//On lance la fenetre d'edition du client
			CreateCustomerDialogView createDialog = new CreateCustomerDialogView( new CreateCustomerDialogControler( new CreateCustomerDialogModel(), null),customerToEdit);
			createDialog.setModal(true);
			createDialog.setVisible(true);
			// Ajout du client dans la liste
			this.model.setCustomers(CustomerTable.getInstance().findAll());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void createNewCustomerButtonActionPerformed(ActionEvent actionEvent) {
		//Customer customersToCreate = this.model.getCustomers().elementAt(selectedIndex);
		CreateCustomerDialogView createDialog = new CreateCustomerDialogView( new CreateCustomerDialogControler( new CreateCustomerDialogModel(), null),null);
		createDialog.setModal(true); 
		createDialog.setVisible(true);
		
		try {
			this.model.setCustomers(CustomerTable.getInstance().findAll());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	public void modifyExistingCustomerButtonActionPerformed(ActionEvent actionEvent, Vector<Integer> selectedIndexes) {
		//Customer customersToCreate = this.model.getCustomers().elementAt(selectedIndex);
		//if(selectedIndexes.size()==1) {
			//for(Integer i : selectedIndexes) {
				Customer c;
				try {
					//c = CustomerTable.getInstance().find(i);
					c = CustomerTable.getInstance().findById(selectedIndexes.elementAt(0));
					//CustomerTable.getInstance().update(c);
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			//}
		//}
		
		CreateCustomerDialogView createDialog = new CreateCustomerDialogView( new CreateCustomerDialogControler( new CreateCustomerDialogModel(), null),null);
		createDialog.setModal(true); 
		createDialog.setVisible(true);
		
		/*
		try {
			this.model.setCustomers(CustomerTable.getInstance().findByIdLike());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
/*
		Vector<Integer> selectedIndexes = new Vector<Integer>();
		//Pour chaque ligne 
		for(int i = 0; i < customersListTable.getRowCount(); i++) {
			// Si la case est coch�e 
			if(customersListTable.getValueAt(i, 4).toString().equals("true")) {
				selectedIndexes.add(Integer.parseInt(meetingsListTable.getValueAt(i, 0).toString()));
			}
		}
		controler.deleteSelectedMeetingsButtonActionPerformed(actionEvent, selectedIndexes);
*/
	}

	public void deleteExistingCustomerButtonActionPerformed(
			ActionEvent actionEvent, Vector<Integer> selectedIndexes) {
		if(selectedIndexes.size() > 0) {
			if(JOptionPane.showConfirmDialog(
					null,
					"Etes-vous sur de vouloir supprimer les infos des clients selectionnes ?",
					"Confirmation", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
				// Suppression
				for(Integer i : selectedIndexes) {
					Customer c;
					try {
						c = CustomerTable.getInstance().find(i);
						CustomerTable.getInstance().delete(c);
						
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				try {
					this.model.setCustomers(CustomerTable.getInstance().findAll());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
}